package cn.jeeweb.modules.client.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.jeeweb.core.common.service.impl.CommonServiceImpl;
import cn.jeeweb.core.query.data.Page;
import cn.jeeweb.core.query.data.PageImpl;
import cn.jeeweb.core.query.data.Pageable;
import cn.jeeweb.core.query.data.Queryable;
import cn.jeeweb.core.query.parse.QueryToWrapper;
import cn.jeeweb.modules.client.entity.ClientOrder;
import cn.jeeweb.modules.client.mapper.TaskMapper;
import cn.jeeweb.modules.client.entity.Task;
import cn.jeeweb.modules.client.service.IClientOrderService;
import cn.jeeweb.modules.client.service.ITaskService;
import cn.jeeweb.modules.client.style.MapImportHandler;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**   
 * @Title: 订单任务
 * @Description: 订单任务
 * @author zhuht
 * @date 2018-04-13 22:05:39
 * @version V1.0   
 *
 */
@Transactional
@Service("taskService")
public class TaskServiceImpl  extends CommonServiceImpl<TaskMapper,Task> implements  ITaskService {

    @Autowired
    private IClientOrderService orderService;

    @Override
    public Page<Task> list(Queryable queryable, Wrapper<Task> wrapper) {
        QueryToWrapper<Task> queryToWrapper = new QueryToWrapper<Task>();
        queryToWrapper.parseCondition(wrapper, queryable);
        queryToWrapper.parseSort(wrapper, queryable);
        Pageable pageable = queryable.getPageable();
        com.baomidou.mybatisplus.plugins.Page<Task> page = new com.baomidou.mybatisplus.plugins.Page<Task>(
                pageable.getPageNumber(), pageable.getPageSize());
        com.baomidou.mybatisplus.plugins.Page<Task> content = selectPage(page, wrapper,queryable.getValue("groupSearch")!=null?queryable.getValue("groupSearch").toString():null);
        return new PageImpl<Task>(content.getRecords(), queryable.getPageable(), content.getTotal());
    }

    public com.baomidou.mybatisplus.plugins.Page<Task> selectPage(com.baomidou.mybatisplus.plugins.Page<Task> page, Wrapper<Task> wrapper,String groupSearch) {
        page.setRecords(baseMapper.selectTaskPage(page, wrapper,groupSearch));
        return page;
    }

    public boolean commitOrder(String userid){
        return retBool(baseMapper.commitOrder(userid));

    }

    public void splitTask(List<Task> tasks,List<ClientOrder> orders){
        updateBatchById(tasks);
        orderService.insertBatch(orders);
    }

    public boolean saveImport(MultipartFile file)throws Exception{
        ImportParams iparams = new ImportParams();
        iparams.setTitleRows(0);
        iparams.setHeadRows(1);
        iparams.setNeedSave(true);
        iparams.setSaveUrl("/home/application/laoke/upload/excel");
        iparams.setDataHandler(new MapImportHandler());
        List<Task> list = ExcelImportUtil.importExcel(file.getInputStream(), Task.class, iparams);
        for(Task task:list){
            if(task.getImgUrl()!=null&&!"".equals(task.getImgUrl())){
                task.setImgUrl(task.getImgUrl().replaceAll("/home/application/laoke",""));
            }
        }
        return this.insertBatch(list);
    }
}
